﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Andorid_Controller : MonoBehaviour
{
    public Android_Kind android_Kind;

    private AndroidInfo androidInfo;
    private NavMeshAgent navMeshAgent;

    private float aimedTime = 0;
    private float blood_full = 0;

    private float shotedTime = 0;

    public Transform shoter;

    public Transform birthPoint;

    RaycastHit hit;

    Vector3 fwd;
    // Start is called before the first frame update
    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();
        switch (android_Kind)
        {
            case Android_Kind.EASY: androidInfo = new AndroidInfo(1, 1, 1,1.5f,1.2f); break;
            case Android_Kind.NORMAL: androidInfo = new AndroidInfo(2, 1, 1,1.5f,1f); break;
            case Android_Kind.HARD: androidInfo = new AndroidInfo(3, 2, 1.5f,1.2f,0.8f); break;
            default: androidInfo = new AndroidInfo(1, 1, 1,1.5f,1.2f); break;
        }
        init();
        
        
    }

    // Update is called once per frame
    void Update()
    {
        if(androidInfo.Blood<blood_full&&Time.time-aimedTime>androidInfo.RebloodTime)
        {
            aimedTime = Time.time;
            androidInfo.Blood += 1;
            if(androidInfo.Blood>blood_full)
            {
                androidInfo.Blood = blood_full;
            }
        }
        fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(transform.position, fwd, out hit, 50))
        {
            if(hit.collider.tag=="player")
            {
                if(Time.time-shotedTime>=androidInfo.ShotSpeed)
                {
                    shotedTime = Time.time;
                    Shoot();
                }
            }
            
        }
            

    }
    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log(collision.gameObject.name);
        if (collision.gameObject.tag == "bullet_player")
        {
            collision.gameObject.GetComponent<BulletController>().Aimed();
            if(androidInfo.Blood>0)
            {
                aimedTime = Time.time;
                androidInfo.Blood -= GameInfo.Instance.GetPlayerForce();
                if(androidInfo.Blood<=0)
                {
                    int newCount = GameInfo.Instance.GetAndroidCount() - 1;
                    GameInfo.Instance.SetAndroidCount(newCount);
                    
                    GameInfo.Instance.onEnemyChange(newCount);
                    gameObject.SetActive(false);
                }
            }
        }
        if(collision.gameObject.tag == "bullet_android")
        {
            collision.gameObject.GetComponent<BulletController>().Aimed();
        }
    }
    void init()
    {
        navMeshAgent.speed = androidInfo.Speed;
        blood_full = androidInfo.Blood;
    }
    void Shoot()
    {
        Transform obj = GameInfo.Instance.AndroidBulletPool.Dequeue();
        obj.position = shoter.position;
        obj.rotation = shoter.rotation;
        obj.gameObject.SetActive(true);
        obj.GetComponent<Rigidbody>().AddForce(obj.up * 1000);
    }
}

